﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HouseAgencySite.Models;
using HouseAgencySite.Utils;
using Webdiyer.WebControls.Mvc;

namespace HouseAgencySite.Areas.Admin.Controllers
{
    public class HtmlContentController : BaseController
    {
        private AgencyDBContext db = new AgencyDBContext();

        //
        // GET: /Admin/HtmlContent/
        [CheckLogin()]
        public ActionResult Index(int pageindex = 1)
        {
            List<HtmlContents> list = db.HtmlContents.Where(o=>o.IsDelete==false).ToList();
            IPagedList pageList = list.ToPagedList(pageindex, 10);
            return View(pageList);
        }

        //
        // GET: /Admin/HtmlContent/Details/5
        [CheckLogin()]
        public ActionResult Details(string id = null)
        {
            HtmlContents htmlcontents = db.HtmlContents.Find(id);
            if (htmlcontents == null)
            {
                return HttpNotFound();
            }
            return View(htmlcontents);
        }

        //
        // GET: /Admin/HtmlContent/Create
        [CheckLogin()]
        public ActionResult Create()
        {
            HtmlContents htmlcontents = new HtmlContents();
            htmlcontents.Guid = Guid.NewGuid().ToString();
            return View(htmlcontents);
        }

        //
        // POST: /Admin/HtmlContent/Create
        [CheckLogin()]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(HtmlContents htmlcontents)
        {
            if (ModelState.IsValid)
            {               
                htmlcontents.Create(htmlcontents, CurrentUser);
                db.HtmlContents.Add(htmlcontents);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(htmlcontents);
        }

        //
        // GET: /Admin/HtmlContent/Edit/5
        [CheckLogin()]
        public ActionResult Edit(string id = null)
        {
            HtmlContents htmlcontents = db.HtmlContents.Find(id);
            if (htmlcontents == null)
            {
                return HttpNotFound();
            }
            return View(htmlcontents);
        }

        //
        // POST: /Admin/HtmlContent/Edit/5
        [CheckLogin()]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(HtmlContents htmlcontents)
        {
            if (ModelState.IsValid)
            {
                db.Entry(htmlcontents).State = EntityState.Modified;
                htmlcontents.Edit(db, htmlcontents, CurrentUser);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(htmlcontents);
        }

        //
        // GET: /Admin/HtmlContent/Delete/5
        [CheckLogin()]
        public ActionResult Delete(string id = null)
        {
            HtmlContents htmlcontents = db.HtmlContents.Find(id);
            if (htmlcontents == null)
            {
                return HttpNotFound();
            }
            return View(htmlcontents);
        }

        //
        // POST: /Admin/HtmlContent/Delete/5
        [CheckLogin()]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            HtmlContents htmlcontents = db.HtmlContents.Find(id);
            db.Entry(htmlcontents).State = EntityState.Modified;
            htmlcontents.Delete(db, htmlcontents, CurrentUser);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [CheckLogin()]
        public ActionResult Search(HtmlContentSearchFilter filter)
        {
            var list = db.HtmlContents.Where(o => o.IsDelete == false);
            if (filter.ModuleType!=0)
            {
                list = list.Where(o => o.ModuleType==filter.ModuleType);
            }
            if (!string.IsNullOrEmpty(filter.Keywords))
            {
                list = list.Where(o => o.Kewords.Contains(filter.Keywords) 
                    ||o.Title.Contains(filter.Keywords)
                    ||o.SubTitle.Contains(filter.Keywords)
                    ||o.ZhaiYao.Contains(filter.Keywords));
            }
            int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);
            return View("Index", list.ToList().ToPagedList(pageIndex, 10));
        }
    }
}